<%#
 Copyright 2013-2018 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
apiVersion: v1
kind: ConfigMap
metadata:
  name: es-config
  namespace: <%= kubernetesNamespace %>
data:
    discovery.type: zen
    minimum.master.nodes: "1"
    unicast.host: jhipster-elasticsearch-discovery
    network.host: 0.0.0.0
    http.enabled: "true"
    memory.opts: -Xms512m -Xmx512m
    cluster.name: jhipster-elasticsearch-cluster
    memory.lock: "false"
    path.data: /es/data
    path.logs: /es/logs
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: jhipster-elasticsearch-master
  namespace: <%= kubernetesNamespace %>
  labels:
    component: jhipster-elasticsearch
    role: master
spec:
  serviceName: jhipster-elasticsearch-master
  replicas: 1
  template:
    metadata:
      labels:
        component: jhipster-elasticsearch
        role: master
    spec:
      initContainers:
      - name: init-sysctl
        image: busybox
        # This would be needed to give access to 'elasticsearch' user that comes from the base
        # image where the provisioned volume is host path (in minikube)
        command:
        - "/bin/sh"
        - "-c"
        - "chown -R 1000:1000 /es"
        - sysctl
        - -w
        - vm.max_map_count=262144
        securityContext:
          runAsUser: 0
        volumeMounts:
        - name: storage
          mountPath: /es
      containers:
      - name: jhipster-elasticsearch-master
        image: <%= DOCKER_JHIPSTER_ELASTICSEARCH %>
        env:
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: node.name
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: cluster.name
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: cluster.name
        - name: discovery.type
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: discovery.type
        - name: node.master
          value: "true"
        - name: discovery.zen.minimum_master_nodes
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: minimum.master.nodes
        - name: discovery.zen.ping.unicast.hosts
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: unicast.host
        - name: node.ingest
          value: "true"
        - name: node.data
          value: "false"
        - name: network.host
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: network.host
        - name: transport.host
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: network.host
        - name: http.enabled
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: http.enabled
        - name: bootstrap.memory_lock
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: memory.lock
        - name: ES_JAVA_OPTS
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: memory.opts
        - name: path.data
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: path.data
        - name: path.logs
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: path.logs
        resources:
          requests:
            memory: "256Mi"
            cpu: "200m"
          limits:
            memory: "512Mi"
            cpu: "400m"
        ports:
        - containerPort: 9300
          name: transport
          protocol: TCP
        - containerPort: 9200
          name: http
          protocol: TCP
        volumeMounts:
        - name: storage
          mountPath: /es
        - name: config
          mountPath: /es/config/
      volumes:
      - name: config
        configMap:
          name: es-config
  volumeClaimTemplates:
  - metadata:
      name: storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: standard
      resources:
        requests:
          storage: 1Gi
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: jhipster-elasticsearch-data
  namespace: <%= kubernetesNamespace %>
  labels:
    component: jhipster-elasticsearch
    role: data
spec:
  serviceName: jhipster-elasticsearch-data
  replicas: 1
  template:
    metadata:
      labels:
        component: jhipster-elasticsearch
        role: data
    spec:
      initContainers:
      - name: init-sysctl
        image: busybox
        command:
        - "/bin/sh"
        - "-c"
        - "chown -R 1000:1000 /es"
        - sysctl
        - -w
        - vm.max_map_count=262144
        securityContext:
          runAsUser: 0
        volumeMounts:
        - name: storage
          mountPath: /es
      containers:
      - name: jhipster-elasticsearch-data
        image: <%= DOCKER_JHIPSTER_ELASTICSEARCH %>
        env:
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: node.name
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: cluster.name
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: cluster.name
        - name: discovery.type
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: discovery.type
        - name: node.master
          value: "false"
        - name: discovery.zen.minimum_master_nodes
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: minimum.master.nodes
        - name: discovery.zen.ping.unicast.hosts
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: unicast.host
        - name: node.ingest
          value: "true"
        - name: node.data
          value: "true"
        - name: network.host
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: network.host
        - name: transport.host
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: network.host
        - name: http.enabled
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: http.enabled
        - name: bootstrap.memory_lock
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: memory.lock
        - name: ES_JAVA_OPTS
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: memory.opts
        - name: path.data
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: path.data
        - name: path.logs
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: path.logs
        resources:
          requests:
            memory: "256Mi"
            cpu: "200m"
          limits:
            memory: "512Mi"
            cpu: "400m"
        ports:
        - containerPort: 9300
          name: transport
          protocol: TCP
        - containerPort: 9200
          name: http
          protocol: TCP
        volumeMounts:
        - name: storage
          mountPath: /es
        - name: config
          mountPath: /es/config/
      volumes:
      - name: config
        configMap:
          name: es-config
  volumeClaimTemplates:
  - metadata:
      name: storage
    spec:
      storageClassName: standard
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: jhipster-elasticsearch-client
  namespace: <%= kubernetesNamespace %>
  labels:
    component: jhipster-elasticsearch
    role: client
spec:
  replicas: 1
  template:
    metadata:
      labels:
        component: jhipster-elasticsearch
        role: client
    spec:
      initContainers:
      - name: init-sysctl
        image: busybox
        command:
        - sysctl
        - -w
        - vm.max_map_count=262144
        securityContext:
          privileged: true
      containers:
      - name: jhipster-elasticsearch-client
        image: <%= DOCKER_JHIPSTER_ELASTICSEARCH %>
        env:
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: node.name
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: cluster.name
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: cluster.name
        - name: discovery.type
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: discovery.type
        - name: node.master
          value: "false"
        - name: discovery.zen.minimum_master_nodes
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: minimum.master.nodes
        - name: discovery.zen.ping.unicast.hosts
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: unicast.host
        - name: node.ingest
          value: "true"
        - name: node.data
          value: "false"
        - name: network.host
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: network.host
        - name: transport.host
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: network.host
        - name: http.enabled
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: http.enabled
        - name: bootstrap.memory_lock
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: memory.lock
        - name: ES_JAVA_OPTS
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: memory.opts
        - name: path.data
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: path.data
        - name: path.logs
          valueFrom:
            configMapKeyRef:
              name: es-config
              key: path.logs
        resources:
          requests:
            memory: "256Mi"
            cpu: "200m"
          limits:
            memory: "512Mi"
            cpu: "400m"
        ports:
        - containerPort: 9300
          name: transport
          protocol: TCP
        - containerPort: 9200
          name: http
          protocol: TCP
        volumeMounts:
        - name: storage
          mountPath: /es
        - name: config
          mountPath: /es/config
      volumes:
      - name: config
        configMap:
          name: es-config
      - name: storage
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: jhipster-elasticsearch-discovery
  namespace: <%= kubernetesNamespace %>
  labels:
    component: jhipster-elasticsearch
    role: master
spec:
  ports:
  - name: transport
    port: 9300
    targetPort: 9300
    protocol: TCP
  selector:
    component: jhipster-elasticsearch
    role: master
---
apiVersion: v1
kind: Service
metadata:
  name: jhipster-elasticsearch-data
  namespace: <%= kubernetesNamespace %>
  labels:
    component: jhipster-elasticsearch
    role: data
spec:
  ports:
  - name: transport
    port: 9300
    targetPort: 9300
    protocol: TCP
  selector:
    component: jhipster-elasticsearch
    role: data
---
apiVersion: v1
kind: Service
metadata:
  name: jhipster-elasticsearch
  namespace: <%= kubernetesNamespace %>
  labels:
    component: jhipster-elasticsearch
    role: client
spec:
  ports:
  - name: http
    port: 9200
    targetPort: 9200
  selector:
    component: jhipster-elasticsearch
    role: client
